package design.simdi.code.final_paper.site.panels.administrare.studenti;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.wicket.Page;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.authorization.strategies.role.Roles;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import design.simdi.code.final_paper.core.entities.Person;
import design.simdi.code.final_paper.core.entities.User;
import design.simdi.code.final_paper.core.enums.UserStatus;
import design.simdi.code.final_paper.core.services.ServiceException;
import design.simdi.code.final_paper.core.services.UserService;
import design.simdi.code.final_paper.site.application.ApplicationRoles;
import design.simdi.code.final_paper.site.application.MyAuthenticatedWebSession;
import design.simdi.code.final_paper.site.pages.BasePage;
import design.simdi.code.final_paper.site.panels.blog.TrimiteMesajPanel;
import design.simdi.code.final_paper.utils.ActionToolbar;
import design.simdi.code.final_paper.utils.MyDataTable;
import design.simdi.code.final_paper.utils.ToolbarItem;
import design.simdi.code.final_paper.utils.YesNoModalWindow;

/**
 * 
 * @author Marius Dumitriu - (Jun 25, 2010 - 7:31:54 PM)
 * 
 */
public class StudentActionToolbar extends ActionToolbar {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@SpringBean
	private UserService userService;

	private YesNoModalWindow deleteYesNoConfirmationModal;

	public StudentActionToolbar(MyDataTable<?> table) {
		super(table);

		deleteYesNoConfirmationModal = new YesNoModalWindow("delete.confirmation", "confirmare2") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onYes(AjaxRequestTarget target) {
				Person k = (Person) getTable().getSelectedRow().getModelObject();
				userService.removeStudentUser(k);
			}

			@Override
			public void onNo(AjaxRequestTarget target) {

			}
		};

		deleteYesNoConfirmationModal.setOutputMarkupPlaceholderTag(true);

		add(deleteYesNoConfirmationModal);
		setOutputMarkupId(true);
	}

	@Override
	public List<ToolbarItem> getItemsList() {
		List<ToolbarItem> lista = new ArrayList<ToolbarItem>();
		Roles roluriCurente = MyAuthenticatedWebSession.get().getRoles();
		if (MyAuthenticatedWebSession.get().isSignedIn()
				&& roluriCurente.containsAll(Arrays.asList(new String[] { ApplicationRoles.ADMIN })))
			lista.add(ToolbarItem.ADD);

		lista.add(ToolbarItem.VIEW);

		if (MyAuthenticatedWebSession.get().isSignedIn()
				&& roluriCurente.containsAll(Arrays.asList(new String[] { ApplicationRoles.ADMIN }))) {
			lista.add(ToolbarItem.MODIFY);
			lista.add(ToolbarItem.DELETE);
			lista.add(ToolbarItem.ACTIVATE);
			lista.add(ToolbarItem.BLOCK);
		}
		if (MyAuthenticatedWebSession.get().isSignedIn())
			lista.add(ToolbarItem.SEND);

		return lista;
	}

	@Override
	public void onAction(ToolbarItem toolbarItem, AjaxRequestTarget target) {

		switch (toolbarItem) {
		case ADD:
			onAddAction(target);
			break;

		case DELETE:
			onDeleteAction(target);
			break;
		case MODIFY:
			onModifyAction(target);
			break;
		case VIEW:
			onViewAction(target);
			break;
		case SEND:
			onSendMessage(target);
			break;
		case BLOCK:
			onBlockUser(target);
			break;
		case ACTIVATE:
			onActivateUser(target);
		}

	}

	private void onActivateUser(AjaxRequestTarget target) {
		try {
			if (getTable().getSelectedRow() == null)
				throw new ServiceException("table.row.not.selected");

			Person p = (Person) getTable().getSelectedRow().getModelObject();

			User u = p.getUser();
			Page page = this.getPage();

			switch (u.getStatus()) {
			case BLOCKED:
			case REQUEST_ACTIVATE:
				userService.changeStatus(u, UserStatus.ACTIVE);
				info(page.getString("user.activated"));
				break;

			default:
				warn(page.getString("user.activated.comment"));
				break;
			}

			target.addComponent(page.get("content"));

		} catch (ServiceException e) {
			warn(getString(e.getMessage()));

		}
	}

	private void onBlockUser(AjaxRequestTarget target) {
		try {
			if (getTable().getSelectedRow() == null)
				throw new ServiceException("table.row.not.selected");

			Person p = (Person) getTable().getSelectedRow().getModelObject();

			User u = p.getUser();
			Page page = this.getPage();

			switch (u.getStatus()) {
			case ACTIVE:
				userService.changeStatus(u, UserStatus.BLOCKED);
				info(page.getString("user.blocked"));
				break;

			default:
				warn(page.getString("user.block.comment"));
				break;
			}

			target.addComponent(page.get("content"));

		} catch (ServiceException e) {
			warn(getString(e.getMessage()));

		}
	}

	private void onSendMessage(AjaxRequestTarget target) {
		try {
			if (getTable().getSelectedRow() == null)
				throw new ServiceException("table.row.not.selected");

			Person p = (Person) getTable().getSelectedRow().getModelObject();

			TrimiteMesajPanel sendPanel = new TrimiteMesajPanel("panel", new Model<Person>(p), this.getPage().get(
					"content:tabs:panel"));
			sendPanel.setOutputMarkupId(true);
			Page page = this.getPage();

			page.get("content:tabs:panel").replaceWith(sendPanel);

			target.addComponent(page.get("content"));

		} catch (ServiceException e) {
			warn(getString(e.getMessage()));

		}

	}

	private void onViewAction(AjaxRequestTarget target) {
		try {
			if (getTable().getSelectedRow() == null)
				throw new ServiceException("table.row.not.selected");

			ViewStudentDetails detailsPanel = new ViewStudentDetails("panel", new Model<Person>((Person) getTable()
					.getSelectedRow().getModelObject()));
			detailsPanel.setOutputMarkupId(true);
			Page page = this.getPage();

			page.get("content:tabs:panel").replaceWith(detailsPanel);

			target.addComponent(page.get("content"));

		} catch (ServiceException e) {
			warn(getString(e.getMessage()));

		}

	}

	private void onModifyAction(AjaxRequestTarget target) {
		try {
			if (getTable().getSelectedRow() == null)
				throw new ServiceException("table.row.not.selected");
			AddStudentWizardPanel addWizardPanel = new AddStudentWizardPanel("panel", (Person) getTable()
					.getSelectedRow().getModelObject());
			addWizardPanel.setOutputMarkupId(true);
			Page page = this.getPage();

			page.get("content:tabs:panel").replaceWith(addWizardPanel);

			target.addComponent(page.get("content"));

		} catch (ServiceException e) {
			warn(getString(e.getMessage()));

		}

	}

	private void onDeleteAction(AjaxRequestTarget target) {

		try {
			if (getTable().getSelectedRow() == null)
				throw new ServiceException("table.row.not.selected");

			deleteYesNoConfirmationModal.show(target);

		} catch (ServiceException e) {
			warn(getString(e.getMessage()));

		}

	}

	private void onAddAction(AjaxRequestTarget target) {

		AddStudentWizardPanel addWizardPanel = new AddStudentWizardPanel("panel", null);
		addWizardPanel.setOutputMarkupId(true);
		Page page = this.getPage();

		page.get("content:tabs:panel").replaceWith(addWizardPanel);

		target.addComponent(page.get("content"));
		target.addComponent(page.get(BasePage.ERRORS_PANEL));

	}

	@Override
	public String getToolbarName(ToolbarItem toolbarItem) {
		return getString(toolbarItem.getName());
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public UserService getUserService() {
		return userService;
	}

}
